package com.xcn.dsxm.main_public.database.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcn.dsxm.main_public.database.dao.DbDao_xcn_admin;
import com.xcn.dsxm.main_public.database.service.DbService_xcn_admin;
import com.xcn.dsxm.main_public.database.service.DbService_xcn_public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DbServiceImpl_xcn_public implements DbService_xcn_public {

    @Autowired
    private DbDao_xcn_admin dbDaoImpl_bx_member;

    @Override
    public int insert(String sql) {
        try {
            return dbDaoImpl_bx_member.insert(sql);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int insert(String mybatisId, HashMap<String, Object> params) {
        try {
            return dbDaoImpl_bx_member.insert(mybatisId, params);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int delete(String sql) {
        try {
            return dbDaoImpl_bx_member.delete(sql);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int delete(String mybatisId, HashMap<String, Object> params) {
        try {
            return dbDaoImpl_bx_member.delete(mybatisId, params);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int update(String sql) {
        try {
            return dbDaoImpl_bx_member.update(sql);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int update(String mybatisId, HashMap<String, Object> params) {
        try {
            return dbDaoImpl_bx_member.update(mybatisId, params);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public JSONArray more(String sql) {
        try {
            List<HashMap<String, Object>> mapResult = dbDaoImpl_bx_member.more(sql);

            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(mapResult));
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JSONArray more(String mybatisId, HashMap<String, Object> params) {
        try {
            List<HashMap<String, Object>> mapResult = dbDaoImpl_bx_member.more(mybatisId, params);

            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(mapResult));
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JSONObject one(String sql) {
        try {
            List<HashMap<String, Object>> mapResult = dbDaoImpl_bx_member.more(sql);

            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(mapResult));
            if (jsonArray != null && jsonArray.size() > 0) {
                return jsonArray.getJSONObject(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JSONObject one(String mybatisId, HashMap<String, Object> params) {
        try {
            List<HashMap<String, Object>> mapResult = dbDaoImpl_bx_member.more(mybatisId, params);

            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(mapResult));
            if (jsonArray != null && jsonArray.size() > 0) {
                return jsonArray.getJSONObject(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int transaction(List<String> sqlArr) {
        try {
            return dbDaoImpl_bx_member.transaction(sqlArr);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int transactions(HashMap<String, Object>... sqlArr) {
        try {
            return dbDaoImpl_bx_member.transactions(sqlArr);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int transactions(List<HashMap<String, Object>> sqlArr) {
        try {
            return dbDaoImpl_bx_member.transactions(sqlArr);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
